﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.ServiceModel.Description;
using System.Runtime.Serialization;
using System.IO;

namespace ChubbyService
{
    #region Service Contract

    [ServiceContract]
    public interface IChubbyServices
    {
        [OperationContract]
        [WebGet(UriTemplate = "Colour", BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json)]
        JSONColour GetColour();
    }
    #endregion

    #region Service Contract for Cross Domain Policies

    [ServiceContract]
    public interface IPolicyRetriever
    {
        [OperationContract, WebGet(UriTemplate = "/clientaccesspolicy.xml")]
        Stream GetSilverlightPolicy();
        [OperationContract, WebGet(UriTemplate = "/crossdomain.xml")]
        Stream GetFlashPolicy();
    }
    #endregion

    #region Data Contracts

    [DataContract]
    public class JSONColour
    {
        [DataMember]
        public string colour = String.Empty;
    }
    #endregion

    #region Service Implementation

    public class ChubbyServices : IChubbyServices, IPolicyRetriever
    {
        public JSONColour GetColour()
        {
            //Return a random colour from the list below
            string[] colours = { "#FFFF0000", "#FF00FF00", "#FF0000FF", "#FFFFFF00", "#FFFF00FF", "#FF00FFFF", "#FF000000" };
            JSONColour colour = new JSONColour();
            Random rand = new Random();
            colour.colour = colours[rand.Next(7)];
            return colour;
        }

        Stream StringToStream(string result)
        {
            WebOperationContext.Current.OutgoingResponse.ContentType = "application/xml";
            return new MemoryStream(Encoding.UTF8.GetBytes(result));
        }

        public Stream GetSilverlightPolicy()
        {
            string result = @"<?xml version=""1.0"" encoding=""utf-8""?>
<access-policy>
    <cross-domain-access>
        <policy>
            <allow-from http-request-headers=""*"">
                <domain uri=""*""/>
            </allow-from>
            <grant-to>
                <resource path=""/"" include-subpaths=""true""/>
            </grant-to>
        </policy>
    </cross-domain-access>
</access-policy>";
            return StringToStream(result);
        }

        public Stream GetFlashPolicy()
        {
            string result = @"<?xml version=""1.0""?>
<!DOCTYPE cross-domain-policy SYSTEM ""http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd"">
<cross-domain-policy>
    <allow-access-from domain=""*"" />
</cross-domain-policy>";
            return StringToStream(result);
        }
    }
    #endregion

    #region Host Implementation

    class Program
    {
        static void Main(string[] args)
        {
            string baseAddress = "http://localhost:8000";

            using (ServiceHost host = new ServiceHost(typeof(ChubbyServices), new Uri(baseAddress)))
            {
                host.AddServiceEndpoint(typeof(IPolicyRetriever), new WebHttpBinding(), "").Behaviors.Add(new WebHttpBehavior());
                host.AddServiceEndpoint(typeof(IChubbyServices), new WebHttpBinding(), "Chubby").Behaviors.Add(new WebHttpBehavior());
                ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
                smb.HttpGetEnabled = true;
                host.Description.Behaviors.Add(smb);

                host.Open();

                Console.WriteLine("WCF Host Running...");
                Console.WriteLine();

                foreach (ServiceEndpoint endpoint in host.Description.Endpoints)
                    Console.WriteLine("Supported Endpoint for {0} at address {1}", endpoint.Binding.GetType().Name, endpoint.Address.Uri);

                Console.WriteLine();
                Console.WriteLine("Press <ENTER> to shutdown");
                Console.ReadLine();

                host.Close();
            }
        }
    }
    #endregion
}
